#include "TestListViewForm.h"
#include <CommCtrl.h>
#include <Zaf\String\ToString.h>



static const wchar_t* g_headerStyleText[] = {
	L"Clickable",
	L"Nonclickable",
	L"None",
};


static const wchar_t* g_sortingTypeText[] = {
	L"None",
	L"Ascending",
	L"Descending",
};


static const wchar_t* g_exStyleText[] = {
	L"FullRowSel",
	L"HasGridLine",
};


TestListViewForm::TestListViewForm() {

	m_listView = nullptr;

	this->CreatedEvent.Register(this, &TestListViewForm::OnCreated);
}



void TestListViewForm::OnCreated(CreatedEventArgs& args) {

	this->SetSize(800, 600);

	m_btnAdd.SetText(L"Add");
	m_btnAdd.SetPosition(400, 0);
	m_btnAdd.SetSize(100, 30);
	m_btnAdd.SetParent(*this);
	m_btnAdd.ClickedEvent.Register(this, &TestListViewForm::OnAddDeleteClicked);
	m_btnAdd.Create();

	m_btnDelete.SetText(L"Delete");
	m_btnDelete.SetPosition(400, 30);
	m_btnDelete.SetSize(100, 30);
	m_btnDelete.SetParent(*this);
	m_btnDelete.ClickedEvent.Register(this, &TestListViewForm::OnAddDeleteClicked);
	m_btnDelete.Create();

	int x = 0;
	for (int index = 0; index != 3; ++index) {

		m_headerStyleButtons[index].SetText(g_headerStyleText[index]);
		m_headerStyleButtons[index].SetPosition(x, 300);
		m_headerStyleButtons[index].SetSize(100, 30);
		m_headerStyleButtons[index].SetParent(*this);
		m_headerStyleButtons[index].Create();
		m_headerStyleButtons[index].ClickedEvent.Register(this, &TestListViewForm::OnRadioButtonClicked);

		x += 100;
	}
	m_headerStyleButtons[0].IsSelected(true);
	m_headerStyleButtons[0].IsGroupHeader(true);

	x = 0;
	for (int index = 0; index != 3; ++index) {

		m_sortingTypeButtons[index].SetText(g_sortingTypeText[index]);
		m_sortingTypeButtons[index].SetPosition(x, 330);
		m_sortingTypeButtons[index].SetSize(100, 30);
		m_sortingTypeButtons[index].SetParent(*this);
		m_sortingTypeButtons[index].Create();
		m_sortingTypeButtons[index].ClickedEvent.Register(this, &TestListViewForm::OnRadioButtonClicked);

		x += 100;
	}
	m_sortingTypeButtons[0].IsSelected(true);
	m_sortingTypeButtons[0].IsGroupHeader(true);

	x = 0;
	for (int index = 0; index != 2; ++index) {

		m_exStyleButtons[index].SetText(g_exStyleText[index]);
		m_exStyleButtons[index].SetPosition(x, 370);
		m_exStyleButtons[index].SetSize(100, 30);
		m_exStyleButtons[index].SetParent(*this);
		m_exStyleButtons[index].Create();
		m_exStyleButtons[index].ClickedEvent.Register(this, &TestListViewForm::OnExStyleButtonClicked);

		x += 100;
	}

	CreateListView();
}



void TestListViewForm::OnAddDeleteClicked(ClickedEventArgs& args) {

	if (args.Sender == &m_btnAdd) {

		std::wstring numText = Zaf::String::ToString<wchar_t>(m_listView->GetItemCount());

		int itemIndex = m_listView->InsertItem(L"Item" + numText);
		m_listView->SetItemText(itemIndex, 1, L"SubItem" + numText);
	}
	else {

		while (true) {

			int selectedIndex = m_listView->GetNextSelectedItemIndex();

			if (selectedIndex == -1) {
				break;
			}

			m_listView->DeleteItem(selectedIndex);
		}
	}
}



void TestListViewForm::OnRadioButtonClicked(ClickedEventArgs& args) {
	CreateListView();
}



void TestListViewForm::OnExStyleButtonClicked(ClickedEventArgs& args) {

	if (args.Sender == &m_exStyleButtons[0]) {
		m_listView->IsFullRowSelect(! m_listView->IsFullRowSelect());
	}
	else if (args.Sender == &m_exStyleButtons[1]) {
		m_listView->HasGridLines(! m_listView->HasGridLines());
	}
}


void TestListViewForm::CreateListView() {

	if (m_listView != nullptr) {
		delete m_listView;
	}

	m_listView = new ListView();

	//Header style
	int itemIndex = m_listView->InsertItem(std::wstring(32, L'A'));
	m_listView->SetItemText(itemIndex, 1, std::wstring(33, L'B'));

	if (m_headerStyleButtons[0].IsSelected() == true) {
		m_listView->SetHeaderStyle(ListViewHeaderStyle::Clickable);
	}
	else if (m_headerStyleButtons[1].IsSelected() == true) {
		m_listView->SetHeaderStyle(ListViewHeaderStyle::Nonclickable);
	}
	else {
		m_listView->SetHeaderStyle(ListViewHeaderStyle::None);
	}

	//Sorting type
	if (m_sortingTypeButtons[0].IsSelected() == true) {
		m_listView->SetSortingType(ListViewSortingType::None);
	}
	else if (m_sortingTypeButtons[1].IsSelected() == true) {
		m_listView->SetSortingType(ListViewSortingType::Ascending);
	}
	else {
		m_listView->SetSortingType(ListViewSortingType::Descending);
	}

	m_listView->SetPosition(0, 0);
	m_listView->SetSize(400, 300);
	m_listView->SetParent(*this);
	m_listView->Create();

	m_listView->InsertColumn(L"Column1", 100);
	m_listView->InsertColumn(L"Column2", 100);
}



LRESULT TestListViewForm::WndProc(const WndMsg& msg) {

	switch (msg.Message) {

	case WM_NOTIFY: {

		LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE)msg.LParam;

		if (lpnmitem->hdr.code == NM_DBLCLK) {

			std::wstring itemText = m_listView->GetItemText(lpnmitem->iItem, lpnmitem->iSubItem);

			std::wstring msg = Zaf::String::ToString<wchar_t>(itemText.length());
			msg.append(1, ',');
			msg.append(itemText);

			MessageBox(
				GetHandle(),
				msg.c_str(),
				nullptr,
				MB_OK
			);
		}

		break;
					}
	}

	return Form::WndProc(msg);
}